/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import classes.Categoria;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jpa.CategoriaJpaController;
import jpa.ReuniaoJpaController;
import jpa.ToDoJpaController;
import utils.JPAUtil;

/**
 *
 * @author Igor
 */
@WebServlet(name = "Agenda", urlPatterns = {"/Agenda"})
public class Agenda extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ToDoJpaController toDoJpa = new ToDoJpaController(JPAUtil.getEMF());
        ReuniaoJpaController reuniaoJpa = new ReuniaoJpaController(JPAUtil.getEMF());
        CategoriaJpaController categoriaJpa = new CategoriaJpaController(JPAUtil.getEMF());
        
        String monthString = request.getParameter("m");
        String yearString = request.getParameter("y");
        
        String pessoaString = (String)request.getSession().getAttribute("Pessoa");
        String categoriaString = (String)request.getSession().getAttribute("Categoria");
        String projetoString = (String)request.getSession().getAttribute("Projeto");
        
        List<String> toDoIntervals = new ArrayList<String>();
        List<String> meetingIntervals = new ArrayList<String>();
        
        Calendar inicio, fim;
        inicio = Calendar.getInstance();
        fim = Calendar.getInstance();
        
        if (monthString != null && yearString != null) {
            request.getSession(true).setAttribute("Year", yearString);
            request.getSession(true).setAttribute("Month", monthString);
            inicio.set(Integer.parseInt(yearString), Integer.parseInt(monthString)-1, 1);
            fim.set(Integer.parseInt(yearString), Integer.parseInt(monthString)-1, inicio.getActualMaximum(Calendar.DAY_OF_MONTH));
        } else {
            Calendar cal = new GregorianCalendar();
            inicio.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1);
            fim.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), inicio.getActualMaximum(Calendar.DAY_OF_MONTH));
        }
        
            //if (pessoaString.equals("Pessoa")) - ainda não funciona
        pessoaString = null;
        if (categoriaString != null && categoriaString.equals(""))
            categoriaString = null;
        //if (projetoString.equals("Projeto")) - ainda não funciona
        projetoString = null;
        
        toDoIntervals = toDoJpa.listarIntervalos(inicio.getTime(), fim.getTime(), pessoaString, categoriaString, projetoString);
        meetingIntervals = reuniaoJpa.listarIntervalos(inicio.getTime(), fim.getTime(), pessoaString, categoriaString, projetoString);
        
        request.setAttribute("ToDoIntervals", toDoIntervals);
        request.setAttribute("MeetingIntervals", meetingIntervals);
        
        List<Categoria> categoriaList = new ArrayList<Categoria>();
        for (Categoria c : categoriaJpa.findCategoriaEntities()){
            categoriaList.add(c);
        }
        request.setAttribute("CategoriaList", categoriaList);
        
        try { 
            request.getRequestDispatcher("calendar.jsp").forward(request, response);
        } 
        catch (Exception ex) {
            request.getRequestDispatcher("error.html").forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
